using System;
using System.Collections.Generic;
using System.Data;

namespace AutoTool
{
    public class UserActivityLogRepository
    {
        private readonly DatabaseHelper _dbHelper;

        public UserActivityLogRepository(string connectionString)
        {
            _dbHelper = new DatabaseHelper(connectionString);
        }

        public List<UserActivityLog> GetAllUserActivityLog()
        {
            string query = "SELECT * FROM UserActivityLog";
            DataTable result = _dbHelper.ExecuteQuery(query);
            return DataMapper.MapToList<UserActivityLog>(result);
        }

        public UserActivityLog GetUserActivityLogById(Guid id)
        {
            string query = "SELECT * FROM UserActivityLog WHERE LogId = @LogId";
            var parameters = new Dictionary<string, object>
            {
                { "@LogId", id }
            };

            DataTable result = _dbHelper.ExecuteQuery(query, parameters);
            if (result.Rows.Count > 0)
            {
                return DataMapper.MapToObject<UserActivityLog>(result.Rows[0]);
            }
            return null;
        }

        public void InsertUserActivityLog(UserActivityLog entity)
        {
            string query = "INSERT INTO UserActivityLog (LogId, UserId, Operation, FileName, FileSize, OperationTime, IPAddress) VALUES (@LogId, @UserId, @Operation, @FileName, @FileSize, @OperationTime, @IPAddress)";
            var parameters = new Dictionary<string, object>
            {
                { "@LogId", entity.LogId },
                { "@UserId", entity.UserId },
                { "@Operation", entity.Operation },
                { "@FileName", entity.FileName },
                { "@FileSize", entity.FileSize },
                { "@OperationTime", entity.OperationTime },
                { "@IPAddress", entity.IPAddress }
            };

            _dbHelper.ExecuteNonQuery(query, parameters);
        }

        public void DeleteUserActivityLog(Guid id)
        {
            string query = "DELETE FROM UserActivityLog WHERE LogId = @LogId";
            var parameters = new Dictionary<string, object>
            {
                { "@LogId", id }
            };

            _dbHelper.ExecuteNonQuery(query, parameters);
        }
    }
}